package com.thinkgem.jeesite.modules.dispatch.service;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.modules.accountinfo.entity.AccountInfo;
import com.thinkgem.jeesite.modules.dispatch.dao.ShopDispatchDao;
import com.thinkgem.jeesite.modules.dispatch.entity.ShopDispatch;
import com.thinkgem.jeesite.modules.dispatch.entity.ShopDispatchOperate;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;

/**
 * 发货单的Service
 * 
 * @author ws
 * @version 2016-12-23
 */
@Service
@Transactional(readOnly = true)
public class ShopDispatchService
        extends CrudService<ShopDispatchDao, ShopDispatch> {

    @Autowired
    private ShopDispatchDao shopDispatchDao;

    @Autowired
    private ShopDispatchOperateService shopDispatchOperateService;
   
    public ShopDispatch findById(String id) {
        return shopDispatchDao.findById(id);
    }

    public ShopDispatch get(String id) {
        return super.get(id);
    }

    public List<ShopDispatch> findList(ShopDispatch shopDispatch) {
        return super.findList(shopDispatch);
    }

    public Page<ShopDispatch> findPage(Page<ShopDispatch> page,
            ShopDispatch shopDispatch) {
        return super.findPage(page, shopDispatch);
    }

    @Transactional(readOnly = false)
    public void save(ShopDispatch shopDispatch) {
        // 发货单操作记录
    	supersave(shopDispatch);
        ShopDispatchOperate shopDispatchOperate = new ShopDispatchOperate();
        shopDispatchOperate.setCreateUser(UserUtils.getUser().getName());// 获取当前登录用户
        shopDispatchOperate.setCreateTime(new Date());
        shopDispatchOperate.setDispatchId(shopDispatch.getId());
        shopDispatchOperate.setStatus(shopDispatch.getStatus());
        shopDispatchOperate.setComment(shopDispatch.getComment());
        shopDispatchOperateService.save(shopDispatchOperate);
    }
    
    @Transactional(readOnly = false)
    public void saves(ShopDispatch shopDispatch,HttpSession session) {
        // 发货单操作记录
    	supersave(shopDispatch);
        ShopDispatchOperate shopDispatchOperate = new ShopDispatchOperate();
        AccountInfo user = (AccountInfo) session.getAttribute("user");// 获取前台当前登录用户
        if(user!=null){
        	shopDispatchOperate.setCreateUser(user.getUsername());
        }
        shopDispatchOperate.setCreateTime(new Date());
        shopDispatchOperate.setDispatchId(shopDispatch.getId());
        shopDispatchOperate.setStatus(shopDispatch.getStatus());
        shopDispatchOperate.setComment(shopDispatch.getComment());
        shopDispatchOperateService.save(shopDispatchOperate);
    }
    
    public void supersave(ShopDispatch shopDispatch) {
    	super.save(shopDispatch);
    }

    @Transactional(readOnly = false)
    public void delete(ShopDispatch shopDispatch) {
        super.delete(shopDispatch);
    }

    public ShopDispatch getById(String id) {
        return shopDispatchDao.findById(id);
    }

	public ShopDispatch getByOrderId(String id) {
	   return shopDispatchDao.getByOrderId(id);
	}

}
